package com.example.dangerouscargodriver.ui.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.example.dangerouscargodriver.ui.fragment.HttpRequestFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabAllFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabCancelFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabCancelFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabCloseFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabCloseFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabDiscussFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabDiscussFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabDoneFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabDoneFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabPublishFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabPublishFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabTransportFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabTransportFragmentWrapper;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.ui.fragment.tab.ResourceManagerTabAllFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MyResourceManagerActivity extends AppCompatActivity {

    private static final String TAG = "ResourceManagerActivity";

    private ActionBar mActionBar = null;
    private TabLayout mTabLayout = null;
    private ViewPager mViewPager = null;

    private String[] mTabs = {"全部", "发布中","议价中", "运输中", "已完成", "取消中", "已失效"};
    private int[] mTabsEnum = {
            ResourceManagerTabAllFragment.TAB_ENUM_ALL,
            ResourceManagerTabAllFragment.TAB_ENUM_PUBLISHING,
            ResourceManagerTabAllFragment.TAB_ENUM_DISCUSSING,
            ResourceManagerTabAllFragment.TAB_ENUM_TRANSPORTING,
            ResourceManagerTabAllFragment.TAB_ENUM_DONE,
            ResourceManagerTabAllFragment.TAB_ENUM_CANCEL,
            ResourceManagerTabAllFragment.TAB_ENUM_LOSE_EFFICACY
    };

    private List<ResourceManagerTabFragment> mTabFragmentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resource_manage);

        mTabLayout = findViewById(R.id.tab_layout);
        mViewPager = findViewById(R.id.view_pager);

        //添加tab
        mTabLayout.addTab(mTabLayout.newTab().setText(mTabs[0]), 0);
        mTabFragmentList.add(new ResourceManagerTabAllFragmentWrapper());

        mTabLayout.addTab(mTabLayout.newTab().setText(mTabs[1]), 1);
        mTabFragmentList.add(new ResourceManagerTabPublishFragmentWrapper());

        mTabLayout.addTab(mTabLayout.newTab().setText(mTabs[2]), 2);
        mTabFragmentList.add(new ResourceManagerTabDiscussFragmentWrapper());

        mTabLayout.addTab(mTabLayout.newTab().setText(mTabs[3]), 3);
        mTabFragmentList.add(new ResourceManagerTabTransportFragmentWrapper());

        mTabLayout.addTab(mTabLayout.newTab().setText(mTabs[4]), 4);
        mTabFragmentList.add(new ResourceManagerTabDoneFragmentWrapper());

        mTabLayout.addTab(mTabLayout.newTab().setText(mTabs[5]), 5);
        mTabFragmentList.add(new ResourceManagerTabCancelFragmentWrapper());

        mTabLayout.addTab(mTabLayout.newTab().setText(mTabs[6]), 6);
        mTabFragmentList.add(new ResourceManagerTabCloseFragmentWrapper());

        /*
        * 这是正常行为.默认情况下，FragmentStatePagerAdapter保留当前显示的片段及其邻居的链接.在第一个适配器上，
        * 创建Fragment0和Fragment1.当您滑动到Fragment1时，他将创建Fragment2并为此调用getItem(2).滑动到Fragment2后，
        * 适配器将销毁Fragment0并创建Fragment3.
        * */
        mViewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager(),
                FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                Log.d(TAG, "position: " +position);
                return mTabFragmentList.get(position);
            }

            @Override
            public int getCount() {

                return mTabFragmentList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {

                return mTabs[position];
            }
        });

        //设置TabLayout和ViewPager联动
        mTabLayout.setupWithViewPager(mViewPager,true);
        Intent intent = getIntent();
        int tab = intent.getIntExtra("TabIndex",0);
        mTabLayout.getTabAt(intent.getIntExtra("TabIndex",0)).select();

        mActionBar = findViewById(R.id.actionBarResourceManage);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}